package me.aartikov.advancedscreenswitchersample;

import me.aartikov.advancedscreenswitchersample.ui.MainAbility;
import me.aartikov.alligator.helpers.MiFraction;
import me.aartikov.alligator.helpers.FractionStack;
import me.aartikov.alligator.util.Log;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.app.Context;

import org.junit.Test;

/**
 * 单元测试
 */
public class ExampleOhosTest extends MainAbility {
    /**
     * 测试包名
     */
    @Test
    public void testBundleName() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        Log.i("单元测试  testBundleName   结果: context:" + context);
    }

    /**
     * 测试创建fragment栈
     */
    @Test
    public void testFragmentStack() {
        FractionStack fragmentStack = FractionStack.getInstance(getFractionManager());
        Log.i("单元测试  testFragmentStack   结果: fragmentStack:" + fragmentStack);
    }

    /**
     * 获取fragment分组
     */
    @Test
    public void testFragmentStackListFragmentGroup() {
        FractionStack fragmentStack = FractionStack.getInstance(getFractionManager());
        fragmentStack.clear();
        for (int i = 0; i < 10; i++) {
            MiFraction fragment = new MiFraction() {
                @Override
                protected void setInitialFragmentIfRequired() {
                }
            };
            fragment.setGouptag(this);
            fragment.setParentId(ResourceTable.Id_main_container);
            fragmentStack.getStackFragment().add(fragment);
        }
        Log.i("单元测试  testFragmentStackListFragmentGroup  添加Ability数据 当前数据量:"
                + fragmentStack.getStackFragment().size());
        for (int i = 0; i < 10; i++) {
            MiFraction fragment = new MiFraction() {
                @Override
                protected void setInitialFragmentIfRequired() {
                }
            };
            fragment.setGouptag(fragment);
            fragment.setParentId(ResourceTable.Id_main_container);
            fragmentStack.getStackFragment().add(fragment);
        }
        Log.i("单元测试  testFragmentStackListFragmentGroup  添加其他数据 当前数据量:"
                + fragmentStack.getStackFragment().size());
        Log.i("单元测试  testFragmentStackListFragmentGroup   查询Ability分组数据:"
                + fragmentStack.getGourpFragment(this.toString()).size());
    }


    /**
     * 清除栈数据
     */
    @Test
    public void testFragmentStackClear() {
        FractionStack fragmentStack = FractionStack.getInstance(getFractionManager());
        for (int i = 0; i < 10; i++) {
            MiFraction fragment = new MiFraction() {
                @Override
                protected void setInitialFragmentIfRequired() {
                }
            };
            fragment.setGouptag(this);
            fragment.setParentId(ResourceTable.Id_main_container);
            fragmentStack.getStackFragment().add(fragment);
        }
        Log.i("单元测试  testFragmentStackClear   添加完成: FragmentStackSize:" + fragmentStack.getStackFragment().size());
        fragmentStack.clear();
        Log.i("单元测试  testFragmentStackClear   clear完成: FragmentStackSize:" + fragmentStack.getStackFragment().size());
    }

    /**
     * 删除分组
     */
    @Test
    public void testFragmentStackRemoveGroup() {
        FractionStack fragmentStack = FractionStack.getInstance(getFractionManager());
        for (int i = 0; i < 10; i++) {
            MiFraction fragment = new MiFraction() {
                @Override
                protected void setInitialFragmentIfRequired() {
                }
            };
            fragment.setGouptag(this);
            fragment.setParentId(ResourceTable.Id_main_container);
            fragmentStack.getStackFragment().add(fragment);
        }
        Log.i("单元测试  testFragmentStackClear   添加完成: 分组:"
                + this + "   当前组数量:" + fragmentStack.getGourpFragment(this.toString()).size());
        for (int i = 0; i < 5; i++) {
            MiFraction fragment = new MiFraction() {
                @Override
                protected void setInitialFragmentIfRequired() {
                }
            };
            fragment.setGouptag(fragment);
            fragment.setParentId(ResourceTable.Id_main_container);
            fragmentStack.getStackFragment().add(fragment);
        }
        Log.i("单元测试  testFragmentStackClear   添加完成  当前总数量:" + fragmentStack.getStackFragment().size());
        fragmentStack.removeAllFragmentByGroup(this.toString());
        Log.i("单元测试  testFragmentStackClear   删除分组后数量" + fragmentStack.getStackFragment().size());

    }


    /**
     * 获取最上层的fragment
     */
    @Test
    public void testgetCurrentFragment() {
        FractionStack fragmentStack = FractionStack.getInstance(getFractionManager());
        for (int i = 0; i < 3; i++) {
            MiFraction fragment = new MiFraction() {
                @Override
                protected void setInitialFragmentIfRequired() {
                }
            };
            fragment.setGouptag(this);
            fragment.setParentId(ResourceTable.Id_main_container);
            fragmentStack.getStackFragment().add(fragment);
            Log.i("单元测试  testgetCurrentFragment   添加fragment:" + fragment);
        }
        Log.i("单元测试  testgetCurrentFragment   获取最上层Fragment:" + fragmentStack.getCurrentFragment());
        Log.i("单元测试  testgetCurrentFragment   删除:" + fragmentStack.getCurrentFragment());
        fragmentStack.getStackFragment().remove(fragmentStack.getCurrentFragment());
        Log.i("单元测试  testgetCurrentFragment   获取最上层Fragment:" + fragmentStack.getCurrentFragment());
    }

}